
/**
智能滚动区域,写了测试
做失败了！！竟然花这么长时间，丢人啊！！
*/
package kgame5.kgui.kcomp {
import kgame5.kgui.GP;
import kgame5.kmath.MathDG;

import flash.display.*;
import flash.events.*;
import flash.geom.*;

public class SmartScroller{
	
	//___________________PROPERTY___________________
	private var 
		mc:Sprite;//自己的实体
			
	private var mapW:Number;
	
	private var rect:Rectangle;
	
	private var aimX:Number=0;
	
	private var EW:Number,EH:Number;
	
	private var aItem:Array;
	//___________________METHOD______________________
	public function SmartScroller(p_EW:Number,p_EH:Number){
					
		mc=new Sprite();
		
		EW=p_EW;
		EH=p_EH;
		
		rect=new Rectangle(0,0,EW,EH);
		var sha:Shape=new Shape();
		GP.fillRect(sha.graphics, 0x00ff00, 0, 0, EW, EH);//做背景上的事件的时候是需要这个的
		mc.addChild(sha);
		
		mc.scrollRect=rect;
		
		
		
		aItem=new Array();
		clear();
		
		mc.addEventListener(Event.ENTER_FRAME, efh);
		mc.addEventListener(MouseEvent.MOUSE_MOVE, mmh);
	}
	public function getDO():Sprite{
		return mc;
	}
	public function clear(){
		
		for(var i:int=0;i<aItem.length;i++){
			mc.removeChild(aItem[i]);
		}
		aItem=new Array();
		
		aimX=0;
		rect.x=0;
		mc.scrollRect=rect;
		mapW=0;
	}
	
	private function mmh(e:MouseEvent){
		aimX=(mapW-EW)*e.localX/EW;
	}
	
	private function efh(e:Event){
		if(rect.x!=aimX){
			rect.x=MathDG.Spring(rect.x, aimX, 3);
			mc.scrollRect=rect;
		}
	}
	
	//mc_item是中心对齐
	public function addItem(mc_item:DisplayObject){
		mc_item.x=mapW+mc_item.width/2;
		mc_item.y=EH/2;
		mapW+=mc_item.width;
		
		mc.addChild(mc_item);
		
		aItem.push(mc_item);
	}
}//end of class
}